package com.lagou;

import com.lagou.filter.TPMonitorFilter;

import java.util.Collections;
import java.util.List;
import java.util.Map;

public class TPReportExecutor implements Runnable {

    @Override
    public void run() {
        System.out.println("开始上报数据");
        synchronized (TPReportExecutor.class) {
            Map<String, List<Long>> requestTime = TPMonitorFilter.getRequestTime();
            for (Map.Entry<String, List<Long>> entry : requestTime.entrySet()) {
                String name = entry.getKey();
                List<Long> timeList = entry.getValue();
                long tp90 = tp(timeList, 0.9d);
                long tp99 = tp(timeList, 0.99d);
                System.out.println("服务：" + name + "\ttp90:" + tp90);
                System.out.println("服务：" + name + "\ttp99:" + tp99);
            }
            TPMonitorFilter.getRequestTime().clear();
        }
    }

    private Long tp(List<Long> list, double tp) {
        int index = (int) (tp * list.size()) - 1;
        Collections.sort(list);
        return list.get(index);
    }
}
